package net.sxnic.item.catalog;

import java.util.List;

import net.sxinfo.core.dao.hibernate3.HibernateCriteria;
import net.sxinfo.core.dao.hibernate3.HibernateDaoUtils;
import net.sxinfo.core.spring25.Dao;
import net.sxinfo.core.spring25.DefaultManager;
import net.sxnic.item.catalog.dao.CatalogDao;

import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service("catalogManager")
public class DefaultCatalogManager extends
		DefaultManager<Catalog, CatalogDao, String> implements CatalogManager {

	@Autowired
	private CatalogDao catalogDao;

	@Override
	protected Dao<Catalog, String> getEntityDao() {
		return catalogDao;
	}

	@Override
	public List<Catalog> findCatalog1() {
		HibernateCriteria hc = new HibernateCriteria().add(HibernateDaoUtils
				.createHibernateOrder("code", true));
		Criterion dc = Restrictions.isNotNull("alias");
		hc.add(dc);
		
		return catalogDao.getByCriteria(hc);
	}

	@Override
	public List<Catalog> findCatalog2ByCatalog1(String catalogAlias) {
		Catalog c = catalogDao.findByUnique("alias", catalogAlias);
		if(c==null){
			return null;
		}
		
		HibernateCriteria hc = new HibernateCriteria().add(HibernateDaoUtils
				.createHibernateOrder("code", true));
		Criterion dc = Restrictions.eq("parent", c);
		hc.add(dc);
		
		return catalogDao.getByCriteria(hc);
	}

}
